package com.mastercard.assets.web.struts.actions;

import com.mastercard.assets.jpa.TblPreimplementacion;
import com.mastercard.assets.jpa.TblPreimplementacionCotiza;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.util.to.UserLogin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class CotizacionesIn
        extends
                Action
{
    public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
    {
        HttpSession session = request.getSession( ) ;
        
        // en caos de null
	if( session == null )
            return mapping.findForward( "noLogin" ) ;
        
        // toma el userLogin
        UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
	// en caso de null
        if( user == null )
            return mapping.findForward( "noLogin" ) ;
        
        List< TblPreimplementacion > preimpl = MasterCardDAO.getAllActivePreimplementationsClosed( ) ,
                preimplClosed = new ArrayList< TblPreimplementacion >( ) ;
        int rol = user.getActualRoleId( ) , cve = 0 , cvePre = 0 ;
        TblPreimplementacionCotiza pre = new TblPreimplementacionCotiza( ) ;
                
        
        for( TblPreimplementacion PI : preimpl )
        {
            // solo para SPONSORSHIPS
            if( PI.getTblFlujo( ).getCveFlujo( ) == 11 || PI.getTblFlujo( ).getCveFlujo( ) == 12 )
            {
                preimplClosed.add( PI ) ;
            } else {
                continue ;
            }
                
            cve = PI.getCvePreimplementacion( ) ;
        }
        
        if( cve != 0 )
        {
            pre = MasterCardDAO.findPreImplCotizaById( cve ) ;
            cvePre = pre.getCveCotizacion( ) ;
        }
        
        MasterCardDAO.closeEntityManager( ) ;
        
        request.setAttribute( "Preimplementations" , preimplClosed ) ;
        request.setAttribute( "rol", rol );
        request.setAttribute( "cvePreCot", "" + cvePre ) ;
        
        return mapping.findForward( "success" ) ;
    }
    
}
